import { postMsg, delay, postFilters, buildMsgHandler } from '../utils.js';

let baseInfoCache;

function postKjzd() {
  let institutionCode = '';
  const clientName = document.querySelector('#sbbName').innerText;
  if (!clientName.includes('企业会计准则')) {
    throw new Error('不支持的财务报表');
  }

  if (clientName.includes('小企业会计准则')) {
    institutionCode = 2;
  } else if (clientName.includes('企业会计准则')) {
    institutionCode = 1;
  }

  postMsg('fixTaxGovInfo', { institutionCode });
}

function beforeCwbb() {
  const title = $(
    '#divSheetlist>li.current_selected_BD',
    window.frames.frmMain.document
  ).text();

  let type = 'balance';
  if (title.includes('资产负债表')) {
    type = 'balance';
  } else if (title.includes('现金流量表')) {
    type = 'cash';
  } else if (title.includes('利润表')) {
    type = 'profit';
  }

  postFilters(type);
  postKjzd();
}

async function doLogin(baseInfo) {
  // window.layer.closeAll();
  // await delay(500);
  // window.loginQieHuan();
  // await delay(500);
  // document.getElementsByClassName('layui-layer-btn0').click();
  // await delay(500);
  // document.getElementById('mmdl_QieHuan').click();
  await delay(500);

  const { nationalTaxId, nationalTaxAccount, nationalTaxPass } =
    baseInfo.companyInfo || {};

  if (!nationalTaxId || !nationalTaxAccount || !nationalTaxPass) {
    console.warn('税局登录信息不完整');
  }
  const form = document.getElementsByTagName('form')[0];
  changeInputValue(form[0], nationalTaxId);
  changeInputValue(form[1], nationalTaxAccount);
  changeInputValue(form[2], nationalTaxPass);

  //   $('#shxydmOrsbh').val(nationalTaxId);
  //   $('#userNameOrSjhm').val(nationalTaxAccount);
  //   $('#passWord').val(nationalTaxPass);
}

function changeInputValue(ele, val) {
  ele.value = val;
  var event = document.createEvent('HTMLEvents');
  event.initEvent("input", true, true);
  event.eventType = 'message';
  ele.dispatchEvent(event);
}

console.info('插件已加载');

if (window.$ && $(document)) {
  $(document).ready(() => {
    dealOption();
  })
} else {
  setTimeout(() => {
    dealOption();
  }, 2000);
}

function dealOption() {
  if (window.location.pathname === '/xxmh/') {
    // document.getElementById('kxText').click()
    setTimeout(() => {
      window.login();
    }, 1000);
    return false;
  }
  postMsg('getBaseInfo');



  const pathname = window.location.pathname;
  if (window.$ && window.$('#frmMain')) {
    $('#frmMain').on('load', () => {
      switch (pathname) {
        // 企业所得税年报
        case '/sbzx-cjpt-web/biz/sb/qysds_a_18nd':
          postFilters('hsqj');
          break;
        // 财务报表
        case '/sbzx-cjpt-web/biz/cb/cwbb_xqykjzz':
        case '/sbzx-cjpt-web/biz/cb/cwbb_qykjzz':
          beforeCwbb();
          break;
        default:
          break;
      }
    });
  }

  const msgHandler = buildMsgHandler({
    loginURLExp: /tpass\.guangdong\.chinatax\.gov\.cn\:8443\/\#\/login/g,
    baseInfo: baseInfoCache,
    onBaseInfo: doLogin,
  });
  window.addEventListener('message', msgHandler, false);
};
